#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
将经纬度数据转换为JSON数组格式
"""

# 原始数据
raw_data = """
116.690926	26.207257
116.999245	39.2377
107.078627	39.375556
113.655165	34.407562
113.217857	24.269597
113.655283	34.596405
116.611269	39.944955
116.520727	39.627569
116.120198	39.614059
108.568696	32.525934
115.532343	33.903580
116.221079	40.058521
116.032125	39.680810
115.121404	33.419770
114.811023	34.045048
106.059418	30.704207
116.221079	40.058521
115.532343	33.903580
114.635231	33.009032
107.904866	34.176030
104.304933	29.662980
107.904866	34.176030
107.398580	33.115554
116.617908	39.729711
116.198957	40.540917
117.394487	39.649219
106.648107	33.183030
116.994155	39.235493
101.748350	26.571735
117.498994	38.827054
115.012441	24.709670
117.727848	39.176215
107.398580	33.115554
109.425361	30.265207
107.398580	33.115554
116.917971	39.570314
124.568276	49.710224
123.848581	49.564372
116.583741	38.138745
109.970115	33.851259
117.304307	38.282334
104.581549	28.770963
114.346832	23.107482
104.356227	28.799358
109.924087	35.186311
116.781728	38.014487
104.581549	28.770963
104.581549	28.770963
110.158058	34.573571
114.638175	26.405287
119.532700	31.776088
111.549827	22.813660
104.879818	29.184793
119.017457	33.492472
87.1103500	44.075987
110.302703	21.194133
110.094224	21.249125
119.179249	34.704604
108.637716	34.354874
90.1795448	44.293587
108.637716	34.354874
118.568036	31.763972
108.557410	34.498274
117.196114	37.761224
108.637716	34.354874
79.8806517	37.288256
116.806434	37.713281
108.637716	34.354874
118.442929	32.057281
106.599353	26.722239
108.505169	34.294188
115.207000	36.238254
114.837601	36.557783
116.806434	37.713281
106.702800	27.148430
108.556066	36.637249
114.998613	36.508456
109.563983	36.555171
116.268758	37.713311
115.692931	38.207711
118.628278	33.892752
108.042971	21.580734
108.637716	34.354874
116.024620	38.041222
118.172356	33.542559
108.796639	27.204124
118.628278	33.892752
116.330582	36.594558
116.279192	39.294972
116.966888	39.959058
122.786628	46.707663
104.580311	35.560478
116.972396	34.293882
103.823473	35.373994
117.056383	39.722440
118.417404	34.405479
117.978781	35.213662
120.002441	33.423349
111.276952	32.416350
109.835118	32.420079
125.388544	41.220188
118.037625	35.721575
125.388544	41.220188
108.376522	24.675875
87.8544200	44.043304
115.148954	38.218627
125.388544	41.220188
114.593507	38.425172
109.701517	32.324092
114.593507	38.425172
109.701517	32.324092
114.314995	38.363168
114.593507	38.425172
119.866473	32.230509
106.861311	27.542874
98.2664965	39.940024
81.1590954	43.978978
118.077026	39.386464
121.895723	39.633939
114.211599	30.355026
113.894720	29.825799
124.085139	40.031921
113.894720	29.825799
108.651848	23.361062
119.785045	31.001073
108.651848	23.361062
117.767765	39.863050
120.817342	30.369293
120.853406	40.856257
103.800806	36.405963
114.716719	37.508066
112.709304	32.082856
115.179298	37.060851
121.110193	41.066414
107.473032	28.547122
115.466728	36.888784
121.110193	41.066414
120.295665	29.231752
120.518215	37.354953
120.981992	41.082964
121.110193	41.066414
113.703988	31.133336
115.370259	37.302277
121.110193	41.066414
111.419622	30.609674
103.982126	36.023312
120.438822	29.078611
114.491887	35.602295
121.110193	41.066414
119.876175	29.603890
114.868016	35.886418
111.419622	30.609674
115.023317	37.030957
101.681462	25.022603
119.942980	28.910241
110.076246	19.936856
123.396061	42.531012
123.396061	42.531012
119.832337	28.356793
114.128890	35.619884
119.832337	28.356793
106.049667	35.234413
122.670708	41.328056
111.365963	30.382639
122.670708	41.328056
112.986136	35.029627
122.670708	41.328056
100.128619	26.057977
114.899346	34.802000
114.804250	34.540701
106.035790	29.929033
100.128619	26.057977
106.636489	29.638476
121.424987	29.598883
121.509331	29.814134
111.618851	33.797774
123.891108	42.329680
121.387408	29.757859
122.740739	42.013188
121.917346	29.361434
112.625523	27.029201
100.493399	39.046218
105.573458	29.393580
121.055280	30.290848
99.7175763	27.753165
110.423151	27.600002
105.847175	30.171871
103.896357	27.763907
118.627921	28.740717
122.428678	40.431161
103.283032	23.734997
102.458449	35.884796
107.665666	29.892596
126.446082	43.900806
110.796503	35.941156
109.843876	31.090417
110.796503	35.941156
109.843876	31.090417
120.504647	28.022670
126.022651	45.013047
108.994741	28.786937
108.876727	30.836960
125.801564	41.668222
120.134949	27.776765
108.876727	30.836960
111.466634	33.246686
112.974726	27.761287
108.876727	30.836960
108.876727	30.836960
125.801564	41.668222
116.116237	30.186512
125.479500	43.862604
104.063773	33.396493
112.384370	39.350281
117.471975	32.902034
111.480893	33.083586
112.384370	39.350281
116.200687	33.087069
116.203850	33.088351
125.362521	45.753290
102.458449	35.884796
107.117506	31.483666
112.215126	37.655664
112.579462	29.591318
103.689211	31.072059
117.089186	31.694864
113.270338	33.953742
112.849175	33.753274
113.364287	33.802437
115.551226	35.867787
115.109953	35.714643
113.212483	27.842658
101.663649	36.634512
101.663649	36.634512
101.663649	36.634512
115.109953	35.714643
113.725376	22.872203
114.096784	22.889031
110.896115	34.618650
118.632495	31.659331
113.487518	23.272790
118.389223	31.254551
112.851310	36.060289
117.906591	33.569269
116.934728	34.145006
115.155319	34.614262
125.335646	49.192873
115.295795	34.401930
113.554067	23.160691
118.543138	30.048083
114.349964	23.103343
125.335646	49.192873
114.346832	23.107482
115.842417	34.365129
114.279964	35.053426
90.9570544	29.565645
110.050736	40.595318
113.872280	35.406252
103.975985	29.212396
113.856920	35.474659
113.615890	35.292724
106.545408	37.375898
117.006414	25.142252
116.482159	23.657811
105.898500	37.887520
113.736516	35.222261
115.860627	23.395803
105.898500	37.887520
105.898500	37.887520
130.485722	46.811569
130.485722	46.811569
118.611346	27.724442
130.485722	46.811569
115.102818	32.146741
113.992994	32.461926
88.9146472	29.302844
116.182884	24.296077
114.934531	31.628081
113.911934	34.165842
131.083913	43.989454
118.290460	25.505080
113.456525	34.264565
104.130434	30.095003
131.012189	45.728167
104.417281	31.497846
104.424482	31.599789
104.713224	31.304235
"""

def convert_to_json():
    """转换经纬度数据为JSON数组"""
    lines = raw_data.strip().split('\n')
    coordinates = []
    
    # 去重
    seen = set()
    unique_coords = []
    
    for line in lines:
        line = line.strip()
        if not line:
            continue
        
        parts = line.split()
        if len(parts) >= 2:
            lng = float(parts[0])
            lat = float(parts[1])
            
            # 去重（保留第一次出现的）
            coord_key = (lng, lat)
            if coord_key not in seen:
                seen.add(coord_key)
                unique_coords.append((lng, lat))
    
    # 生成JSON格式
    print(f"# 原始数据: {len(lines)} 条")
    print(f"# 去重后: {len(unique_coords)} 条")
    print(f"\ncoordinates = [")
    
    for i, (lng, lat) in enumerate(unique_coords):
        print(f"    {{")
        print(f'        "lng": {lng},')
        print(f'        "lat": {lat},')
        print(f'        "level": 17,')
        print(f'        "name": "位置{i+1}"')
        if i < len(unique_coords) - 1:
            print(f"    }},")
        else:
            print(f"    }}")
    
    print(f"]")
    
    # 同时保存到文件
    with open('coordinates_output.txt', 'w', encoding='utf-8') as f:
        f.write(f"# 原始数据: {len(lines)} 条\n")
        f.write(f"# 去重后: {len(unique_coords)} 条\n\n")
        f.write("coordinates = [\n")
        
        for i, (lng, lat) in enumerate(unique_coords):
            f.write("    {\n")
            f.write(f'        "lng": {lng},\n')
            f.write(f'        "lat": {lat},\n')
            f.write(f'        "level": 17,\n')
            f.write(f'        "name": "位置{i+1}"\n')
            if i < len(unique_coords) - 1:
                f.write("    },\n")
            else:
                f.write("    }\n")
        
        f.write("]\n")
    
    print(f"\n✅ 已保存到 coordinates_output.txt")
    print(f"📊 统计:")
    print(f"  - 原始: {len(lines)} 条")
    print(f"  - 去重后: {len(unique_coords)} 条")
    print(f"  - 重复: {len(lines) - len(unique_coords)} 条")

if __name__ == "__main__":
    convert_to_json()






